//
//  HomeServer.m
//  CYWFrame
//
//  Created by Macx on 2017/8/4.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "HomeServer.h"

#import "Posts.h"

#import "UIView+LZCView.h"

@implementation HomeServer
//{
//    NSMutableArray *_mArr;
//}
//
//- (NSMutableArray *)homeAllPostTbaleView:(UITableView *)tableView {
//    
//    _mArr = [[NSMutableArray alloc] init];
//    
//    [[NetworkTool sharedTool] requestWithName:@"获取动态" URLString:APIPosts parameters:@{@"per_page":@"3", @"page":@"1"} method:@"GET" callBack:^(id responseObject) {
//        
//        for (NSDictionary *dic in responseObject[@"data"]) {
//            Posts *model = [[Posts alloc] init];
//            [model setValuesForKeysWithDictionary:dic];
//            [_postArr addObject:model];
//        }
//        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [tableView reloadData];
//        });
//    }];
//    
//}
//

+ (void)focus:(UIButton *)btn toId:(NSString *)toId {
    
    UIViewController *currVC = [[[UIView alloc] init] topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
    
    if (GetUserDefaule(USERINFO) == NULL) {
        [currVC presentViewController:[LZCTools alertMessage:@"请先登录"]animated:YES completion:nil];
        return;
    }
    
    
    if ([btn.titleLabel.text isEqualToString:@"  关注"] || [btn.titleLabel.text isEqualToString:@"关注"]) {
        [[NetworkTool sharedTool] requestWithName:@"关注" URLString:APIFavorites parameters:@{@"to":toId} method:@"POST" callBack:^(id responseObject) {
            [btn setBackgroundImage:IMAGE(@"btn_concerned_bg") forState:UIControlStateNormal];
            [btn setImage:[IMAGE(@"btn_ed_concern") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [btn setTitle:@"已关注" forState:UIControlStateNormal];
        }];
    }
    else {
        NSString *url = [NSString stringWithFormat:@"%@/%@", APIFavorites, toId];
        [[NetworkTool sharedTool] requestWithName:@"取消关注" URLString:url parameters:nil method:@"DELETE" callBack:^(id responseObject) {
            [btn setBackgroundImage:IMAGE(@"btn_unconcern_bg") forState:UIControlStateNormal];
            [btn setImage:[IMAGE(@"btn_un_concern") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [btn setTitle:@"  关注" forState:UIControlStateNormal];
        }];
    }
}

+ (void)praise:(UIButton *)btn postId:(NSString *)postId url:(NSString *)url {
    
    UIViewController *currVC = [[[UIView alloc] init] topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
    
    if (GetUserDefaule(USERINFO) == NULL) {
        [currVC presentViewController:[LZCTools alertMessage:@"请先登录"]animated:YES completion:nil];
        return;
    }
    
    
    NSInteger num = [btn.titleLabel.text intValue];
    
    NSData *data = UIImagePNGRepresentation(btn.imageView.image);
    NSData *data1 = UIImagePNGRepresentation(IMAGE(@"index_zan"));
    NSData *data2 = UIImagePNGRepresentation(IMAGE(@"index_zan_ed"));
    
    if ([data isEqual:data1]) {
        // POST /posts/{id}/praises 动态点赞 不能重复点赞 id 动态ID
        [[NetworkTool sharedTool] requestWithName:@"点赞" URLString:url parameters:nil method:@"POST" callBack:^(id responseObject) {
            [btn setImage:[IMAGE(@"index_zan_ed") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [btn setTitle:[NSString stringWithFormat:@" %ld", num+1] forState:UIControlStateNormal];
            
            if ([postId isEqualToString:@"文章"]) {
                [btn setTitle:@"已赞" forState:UIControlStateNormal];
            }
        }];
    }
    else if ([data isEqual:data2]){
        // POST /posts/{id}/praises 动态点赞 不能重复点赞 id 动态ID
        [[NetworkTool sharedTool] requestWithName:@"取消点赞" URLString:url parameters:nil method:@"DELETE" callBack:^(id responseObject) {
            [btn setImage:[IMAGE(@"index_zan") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];            [btn setTitle:[NSString stringWithFormat:@"%ld", num-1] forState:UIControlStateNormal];
            if ([postId isEqualToString:@"文章"]) {
                [btn setTitle:@"点赞" forState:UIControlStateNormal];
            }
        }];

    }
    
        
}


+ (void)praiseLogicCurrBtn:(UIButton *)currBtn otherBtn:(UIButton *)otherBtn url:(NSString *)url {

    UIViewController *currVC = [[[UIView alloc] init] topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
    
    if (GetUserDefaule(USERINFO) == NULL) {
        [currVC presentViewController:[LZCTools alertMessage:@"请先登录"]animated:YES completion:nil];
        return;
    }
    
    
    NSInteger num = [currBtn.titleLabel.text intValue];
    NSInteger oNum = [otherBtn.titleLabel.text intValue];
    
    NSData *data = UIImagePNGRepresentation(currBtn.imageView.image);
    NSData *oData = UIImagePNGRepresentation(otherBtn.imageView.image);
    NSData *data1 = UIImagePNGRepresentation(IMAGE(@"question_cai"));
    NSData *data2 = UIImagePNGRepresentation(IMAGE(@"index_yicai"));
    NSData *data3 = UIImagePNGRepresentation(IMAGE(@"index_zan"));
    NSData *data4 = UIImagePNGRepresentation(IMAGE(@"index_zan_ed"));
    
    // 赞
    if ([data isEqual:data3]) {
        [[NetworkTool sharedTool] requestWithName:@"点赞" URLString:url parameters:nil method:@"POST" callBack:^(id responseObject) {
            [currBtn setImage:[IMAGE(@"index_zan_ed") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [currBtn setTitle:[NSString stringWithFormat:@" %ld", num+1] forState:UIControlStateNormal];
            
            if ([oData isEqual:data2]) {
                [otherBtn setImage:[IMAGE(@"question_cai") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
                [otherBtn setTitle:[NSString stringWithFormat:@" %ld", oNum-1] forState:UIControlStateNormal];
            }
            
        }];
    }
    else if ([data isEqual:data4]){
        [[NetworkTool sharedTool] requestWithName:@"取消点赞" URLString:url parameters:nil method:@"DELETE" callBack:^(id responseObject) {
            [currBtn setImage:[IMAGE(@"index_zan") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [currBtn setTitle:[NSString stringWithFormat:@"%ld", num-1] forState:UIControlStateNormal];
            
        }];
    }
    // 踩
    else if ([data isEqual:data1]) {
        [[NetworkTool sharedTool] requestWithName:@"踩" URLString:url parameters:nil method:@"POST" callBack:^(id responseObject) {
            [currBtn setImage:[IMAGE(@"index_yicai") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [currBtn setTitle:[NSString stringWithFormat:@" %ld", num+1] forState:UIControlStateNormal];
            
            if ([oData isEqual:data4]) {
                [otherBtn setImage:[IMAGE(@"index_zan") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
                [otherBtn setTitle:[NSString stringWithFormat:@" %ld", oNum-1] forState:UIControlStateNormal];
            }
        }];
    }
    else if ([data isEqual:data2]){
        [[NetworkTool sharedTool] requestWithName:@"取消踩" URLString:url parameters:nil method:@"DELETE" callBack:^(id responseObject) {
            [currBtn setImage:[IMAGE(@"question_cai") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [currBtn setTitle:[NSString stringWithFormat:@"%ld", num-1] forState:UIControlStateNormal];
            
        }];
    }

}

@end
